package com.hmy.finance.service.core.dto;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.hmy.ccp.common.BasePage;
import com.hmy.finance.api.bo.core.request.enums.StatementTypeEnum;
import com.hmy.finance.bo.request.ReceivablesDeviceQueryRequest;
import com.hmy.finance.bo.request.ReceivablesDeviceQueryRequest.ContractDeviceDetailRequest;
import com.hmy.finance.dal.po.DeviceRentalRecordQueryPo;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Data;

/**
 * 对账管理 - 设备拉取 - 金额汇总统计查询dto
 */
@Data
public class ReceivablesDeviceQueryDto extends BasePage {

    /**
     * 账单类型:月结、期结、总账单 MONTHLY("1", "月结"), PERIODIC("2", "期结"), TOTAL("3", "总账单"), FREE("4", "自由账单"), FLEXIBLE("5","灵活账单"); {@link StatementTypeEnum}
     */
    private String statementType;

    /**
     * 账单开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime statementStartTime;

    /**
     * 账单结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime statementEndTime;

    /**
     * 设备选取列表
     */
    private List<ContractDeviceDetailDto> deviceDetails;

    /**
     * 设备租赁记录query
     */
    private List<DeviceRentalRecordQueryPo> deviceRentalRecordQueryPoList;

    /**
     * 设备选取列表
     */
    @Data
    public static class ContractDeviceDetailDto {

        /**
         * 合同id
         */
        private Long contractId;

        /**
         * 设备id
         */
        private Long deviceId;

        /**
         * 设备租赁记录ID
         */
        private Long deviceRentalRecordId;

        /**
         * 合同编号
         */
        private String contractNo;

        /**
         * 设备编号
         */
        private String deviceNo;

        /**
         * 账单开始时间
         */
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        private LocalDateTime setPreStartTime;

        /**
         * 账单结束时间
         */
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        private LocalDateTime setPreEndTime;
    }

    /**
     * 参数转换
     *
     * @param request
     * @return
     */
    public static ReceivablesDeviceQueryDto convertToDto(ReceivablesDeviceQueryRequest request) {
        ReceivablesDeviceQueryDto queryDto = BeanUtil.copyProperties(request, ReceivablesDeviceQueryDto.class);
        queryDto.setDeviceRentalRecordQueryPoList(build(request.getDeviceDetails()));

        return queryDto;
    }

    private static List<DeviceRentalRecordQueryPo> build(List<ContractDeviceDetailRequest> contractDeviceDetailRequestList) {
        return BeanUtil.copyToList(contractDeviceDetailRequestList, DeviceRentalRecordQueryPo.class);
    }

}
